package com.dm.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dm.common.JsonResult;
import com.dm.entity.Manager;
import com.dm.service.IManagerService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dancun
 * @since 2021-05-02
 */
@RestController
@Slf4j
@RequestMapping("/manager")
public class ManagerController {

        @Autowired
        IManagerService managerService;


        @PostMapping("/login")
        @ApiOperation("")
        public JsonResult save(@RequestBody Manager manager){
            QueryWrapper<Manager> wrapper = Wrappers.query();
            wrapper.eq("pwd",manager.getPwd());
            wrapper.eq("uname",manager.getUname());
            try{
               manager= managerService.list(wrapper).get(0);
            }catch (Exception e){
               e.printStackTrace();
                return JsonResult.renderError();
            }
            if(manager==null)
                return JsonResult.renderError();
            return JsonResult.renderSuccess();
        }

    @PostMapping("/reg")
    @ApiOperation("")
    public JsonResult reg(@RequestBody Manager manager){
        try{
            managerService.save(manager);
        }catch (Exception e){
           e.printStackTrace();
            return JsonResult.renderError();
        }
        return JsonResult.renderSuccess();
    }

        @PostMapping("/update")
        @ApiOperation("")
        public JsonResult update(@RequestBody Manager manager){
            try{
                managerService.updateById(manager);
            }catch (Exception e){
                e.printStackTrace();
                return JsonResult.renderError();
            }
            return JsonResult.renderSuccess();
        }

    @PostMapping("/list")
    @ApiOperation("")
    public JsonResult list(){
        List<Manager> list = new ArrayList<>();
        try{
           list =  managerService.listManager(Wrappers.query());
        }catch (Exception e){
           e.printStackTrace();
            return JsonResult.renderError();
        }
        return JsonResult.renderSuccess(list);
    }





}
